package com.lzs.bootsafety.business.sys.controller;

import com.alibaba.fastjson.JSON;
import com.lzs.bootsafety.business.sys.entity.SysMenu;
import com.lzs.bootsafety.common.base.controller.BaseController;
import com.lzs.bootsafety.common.util.MenuTree;
import com.lzs.bootsafety.common.util.Resu;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 * @author zhanshi liu
 * @date 2020/03/24
 * @website www.jhmsona.cn
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController {
    //获取每个认证用户的菜单导航
    @GetMapping("/getMenuByUserId")
    public Resu getMenuByUserId(){
        List<SysMenu> sysMenuList = sysMenuService.getListMenuByUserId(getUserId(),"A");
        return Resu.suces().put("data",MenuTree.buildTreeNormal(sysMenuList, (long) 0));
    }
    /** 查询所有菜单 */
    @GetMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public Resu getList(@RequestParam(value = "data",required = false) String  data){
        SysMenu sysMenu = JSON.parseObject(data,SysMenu.class);
        List<SysMenu> sysMenuList = sysMenuService.getList(sysMenu);
        if (null == sysMenu){
            return Resu.suces().put("data",MenuTree.buildTree(sysMenuList, (long) 0));
        }else if(checkIsNull.objCheckIsNull(sysMenu)){
            return Resu.suces().put("data",MenuTree.buildTree(sysMenuList, (long) 0));
        }else{
            return Resu.suces().put("data",sysMenuList);
        }
    }
    /** 查询状态正常的*/
    @GetMapping("/listNormal")
    public Resu getList(){
        List<SysMenu> sysMenuList = sysMenuService.getList(null);
        return Resu.suces().put("data",MenuTree.buildTreeNormal(sysMenuList, (long) 0));
    }
    /**删除*/
    @DeleteMapping("/del")
    @RequiresPermissions("sys:menu:del")
    public Resu del(Long menuId){
        List<SysMenu> sysMenuList = sysMenuService.getChildrenByMenuId(menuId);
        if (sysMenuList.size() > 0){
            return Resu.error("有子菜单或按钮,不能删除");
        }
        return respDel(sysMenuService.delByPrimaryKey(menuId));
    }
    @PutMapping("/modify")//待添加验证
    @RequiresPermissions("sys:menu:modify")
    public Resu modify(@RequestBody SysMenu sysMenu){
        sysMenu.setUpdatePeople(getUserName());
        sysMenu.setUpdateTime(date());
        int r = sysMenuService.modifyByPrimaryKeySelective(sysMenu);
        return respudp(r);
    }
    @PostMapping("/add")//待添加验证
    @RequiresPermissions("sys:menu:add")
    public Resu add(@RequestBody SysMenu sysMenu){
        sysMenu.setCreatePeople(getUserName());
        sysMenu.setCreateTime(date());
        int r = sysMenuService.addSelective(sysMenu);
        return respAdd(r);
    }
    @GetMapping("/info")
    public Resu info(Long menuId){
        SysMenu sysMenu = sysMenuService.getSysMenuInfo(menuId);
        return Resu.suces().put("data",sysMenu);
    }

}
